<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-02 10:07:38
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-02-14 21:08:20
 * ------------------------------------------------ *
 * 职员模型                                         *
 * ------------------------------------------------ */
namespace Admin\Model;

class ManagersModel extends CommonModel {

	/* 登录验证并获取用户信息
	 * ----------------------- */
	public function checkLogin() {
		$m     = M('managers');
		$manager = $m->where('loginName="' . I('loginName') . '" and managerFlag=1 and managerStatus=1')->find();
		if(empty($manager)){
			return false;
		}
		cookie("loginName", I('loginName'), 3600*24*60);
		if ($manager['loginPwd'] != md5(I('loginPwd') . $manager['secretKey'])) {
			return false;
		}
		//取得权限列表
		$role   = M('roles')->where('roleFlag =1 and roleId=' . $manager['managerRoleId'])->find();
		$manager['roleName'] = $role['roleName'];
		$manager['grant']    = explode(',', $role['grant']);				
		if(I('remember') == "1"){			
			cookie("loginPwd", md5($manager['loginPwd']),3600*24*60);
		}else{		
			cookie("loginPwd", null);
		}
		//删除敏感数据
		unset($manager['loginPwd']);
		unset($manager['secretKey']);

		$m->lastTime       = date('Y-m-d H:i:s');
		$m->lastIP         = get_client_ip();
		$m->where(' managerId=' . $manager['managerId'])->save();
		//记录登录日志
		$data              = array();
		$data["managerId"]   = $manager['managerId'];
		$data["loginTime"] = date('Y-m-d H:i:s');
		$data["loginIp"]   = get_client_ip();
		$m                 = M('log_manager_logins');
		$m->add($data);
		return $manager;
		
	}

	/* 获取职员信息cookie保存密码
	 *--------------------------*/
	public function getManagerInfo(){
	 	$manager = M('managers')->where('loginName="' . cookie('loginName') . '" and managerFlag=1 and managerStatus=1')->find();
	 	if(empty($manager) || cookie('loginPwd')!=md5($manager['loginPwd'])){
			return false;
		}
	 	
	 	//获取角色权限
		$role  = M('roles')->where('roleFlag =1 and roleId=' . $manager['managerRoleId'])->find();
		$manager['roleName'] = $role['roleName'];
		$manager['grant']    = explode(',', $role['grant']);
		unset($manager['loginPwd']);
		unset($manager['secretKey']);
	 	
	    return $manager;
	}


	/* 改变状态
	 *--------------------------*/
	public function editStatus($name){
		$managerId = (int)I('managerId');
	 	if($managerId==1  || $managerId==session('admin.namagerId')) {
			return false;
		}
	 	$m = M('managers');
	 	$m->$name = (I($name)==1)?1:0;
	 	$rs = $m->where("managerId=".(int)I('managerId',0))->save();
	    if(false !== $rs){
			return true;
		}
	 	return false;
	}


	/* 添加职员
	 *--------------------------*/
	 public function addManagers($managerPhoto){

		$data = array();
		$data["loginName"] = I("loginName");
		$data["secretKey"] = rand(1000,9999);
		$data["loginPwd"] = md5(I("loginPwd").$data["secretKey"]);
		
		$data["managerRoleId"] = (int)I("managerRoleId");
		$data["workStatus"] = (int)I("workStatus");
		$data["managerStatus"] = (int)I("managerStatus");
		$data["managerFlag"] = 1;
		$data["createTime"] = date('Y-m-d H:i:s');
	    if($this->checkEmpty($data)){
	    	$data["managerName"] = I("managerName");
	    	$data["managerNo"] = I("managerNo");
	    	$data["managerPhoto"] = 'avatar/avatar.jpg';
	    	if(!empty($_FILES['managerPhoto']['tmp_name'])){
				$managerPhoto=$this->upAvatar();
				if($managerPhoto==false){
					return false;
				}
				$data["managerPhoto"] = $managerPhoto;
			}
	    	
			$m = M('managers');
			$rs = $m->add($data);
			if(false !== $rs){
				return true;
			}
			if(!empty($managerPhoto)){
				$this->delAvatar($managerPhoto);
			}
		}
		return false;
	 } 


	 /* 编辑职员
	 *--------------------------*/
	 public function editManagers(){
		
	 	$managerId = (int)I("managerId",0);
	 	$m = M('managers');
	 	$oldPhoto = $m->where("managerId=".$managerId)->getField('managerPhoto');
	 	
		$data = array();
		$data["managerRoleId"] = (int)I("managerRoleId");
		$data["workStatus"] = (int)I("workStatus");
		$data["managerStatus"] = (int)I("managerStatus");
		
	    if($this->checkEmpty($data)){
	    	$data["managerName"] = I("managerName");
	    	$data["managerNo"] = I("managerNo");
	    	if(!empty($_FILES['managerPhoto']['tmp_name'])){
				$managerPhoto=$this->upAvatar();
				if($managerPhoto==false){
					return false;
				}
				$data["managerPhoto"] = $managerPhoto;
			}
			if(!empty(I("loginPwd"))){
				$data["secretKey"] = rand(1000,9999);
				$data["loginPwd"] = md5(I("loginPwd").$data["secretKey"]);
	    	}
			
			$rs = $m->where("managerId=".$managerId)->save($data);
			if(false !== $rs){
				$currentId=(int)session('admin.managerId');
				if($managerId==$currentId){
					session('admin.managerRoleId',$data["managerRoleId"]);
					session('admin.managerName',$data["managerName"]);
					session('admin.managerNo',$data["managerNo"]);
					if(!empty($data["managerPhoto"])){
						session('admin.managerPhoto',$data["managerPhoto"]);
						if($oldPhoto!='avatar/avatar.jpg'){
							$this->delAvatar($oldPhoto);
						}
					}
				}				
				return true;
			}
			if(!empty($managerPhoto)){
				$this->delAvatar($managerPhoto);
			}
		}
		return false;
	 } 


	 public function delManagers(){
	 	$managerId = (int)I('managerId');
	 	if($managerId==1  || $managerId==session('admin.namagerId')) {
			return false;
		}
	 	$m = M('managers');
	 	$m->managerFlag=-1;
	    $rs = $m->where("managerId=".$managerId)->save();
		if(false !== $rs){
			return true;
		}
		return false;
	 }


}
